package deliciouse.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import deliciouse.dao.CategoryDao;
import deliciouse.entity.Category;

public class CategoryDaoImpl implements CategoryDao {
	
	private static class CategoryRowMapper implements RowMapper<Category>{
		public Category mapRow(ResultSet rs) throws SQLException {
			Category c = new Category();
			c.setId(rs.getInt("Id"));
			c.setName(rs.getString("Name"));
			return c;
		}
	}
	
	private JdbcTemplate db = new JdbcTemplate();

	public List<Category> getAll(){
		return db.queryList("select * from Category", new CategoryRowMapper());
	}
	
	public Category fetchById(int id) {
		List<Category> list = db.queryList("select * from Category where Id=?", new CategoryRowMapper(), id);
		return list.size()>0?list.get(0):null;
	}

	public void update(Category category) {
		db.executeUpdate("update Category set Name=? where Id=?",
				category.getName(), category.getId());
	}
	
	public static void main(String[] args) {
		for(Category c : new CategoryDaoImpl().getAll()){
			System.out.println(c.getName());
		}
	}
	
	
}
